function oz_lsq_lad_fit,x,y

;+
; NAME:
;       OZ_LSQ_LAD_FIT
;
; PURPOSE:
;       Pezrforms two types of fit (least squares and least
;       abs. deviation) to bivariate data. Returns (intercept,
;       gradient, stddev) of fits.
;
; CALLING SEQUENCE:
;       OZ_LSQ_LAD_FIT(X,Y)
;
; INPUTS:
;       X, Y
;
; OUTPUTS:
;       The integer index into the list (starting from zero)
;
; MODIFICATION HISTORY:
;       Started by Rob Crain, Swinburne, 07-Mar-2010
;-

; check x and y are the same length
nx = n_elements(x)
ny = n_elements(y)
if (nx NE ny)then begin
   if keyword_set(verbose) then print,'[oz_lsq_lad] x and y data not the same length'
   return,-1
endif

; compute fits
lsq = linfit(x,y)
lad = ladfit(x,y)

; also compute the stddev of the data wrt to each fit
devs = fltarr(2,nx)
for i=0,nx-1 do begin
   fit_value   = [lsq[0] + lsq[1]*x[i],lad[0] + lad[1]*x[i]]
   devs[0,i] = y[i] - fit_value[0]
   devs[1,i] = y[i] - fit_value[1]
endfor
lsq = [lsq,stddev(devs[0,*])]
lad = [lad,stddev(devs[1,*])]

; package into a structure
data = create_struct('lsq',lsq,'lad',lad)

return,data

end
